package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import model.EventModel;

public class Events {

	public Events() {}
	
	
	public List<EventModel> ListEvents() {
		List<EventModel> list = new ArrayList<EventModel>();
		Connection con = new Connect().getConnection();
		String sql = "SELECT * FROM EVENT ORDER BY ID DESC";
		try {
			PreparedStatement state = con.prepareStatement(sql);
			ResultSet rs = state.executeQuery();
			while (rs.next()) {
				list.add(new EventModel(
						rs.getString("id"),
						rs.getString("name"),
						rs.getString("event_group_id")));
			}
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	public boolean UpdateEvent(EventModel e) {
		Connection con = new Connect().getConnection();
		String sql = "UPDATE EVENT SET ID = '" + e.getId()+ "', NAME = '"+ e.getName()+ "',EVENT_GROUP_ID = '"+e.getEvent_group_id()+"'";
		try {
			PreparedStatement state = con.prepareStatement(sql);
			state.executeUpdate();
			con.close();
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
		return false;
	}

	public void DeleteEvent(String id) {
		Connection con = new Connect().getConnection();
		String sql = "DELETE FROM EVENT WHERE ID = '" + id + "'";
		try {
			PreparedStatement state = con.prepareStatement(sql);
			state.executeUpdate();
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public boolean CreateEvent(EventModel e) {
		Connection con = new Connect().getConnection();
		String sql = "INSERT INTO EVENT VALUES('" + e.getId()+ "', '" + e.getName()+"', '"+e.getEvent_group_id()+"')";
		try {
			PreparedStatement state = con.prepareStatement(sql);
			state.executeUpdate();
			con.close();
			return true;
		} catch (SQLException ex) {
			ex.printStackTrace();
		}
		return false;
	}

	public boolean CheckExistEvent(String id) {
		Connection con = new Connect().getConnection();
		String sql = "SELECT ID FROM EVENT WHERE ID = '" +id 
				+ "'";
		try {
			PreparedStatement state = con.prepareStatement(sql);
			ResultSet rs = state.executeQuery();
			if (rs.next())
				return true;
			con.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}

}
